import { useState, useCallback } from 'react';
import produce from 'immer';
function useRowVisual(primaryKey) {
    const [rowsVisual, setRowsVisual] = useState([]);
    const modifyRowsVisual = useCallback((id, key, value) => {
        setRowsVisual((prevState) => {
            let nextState = produce(prevState, draftState => {
                let matchedIndex = draftState.findIndex((d) => { return d[primaryKey] === id });
                let matchedRow = matchedIndex > -1 ? draftState[matchedIndex] : { [primaryKey]: id };
                draftState[matchedIndex > -1 ? matchedIndex : 0] = { ...matchedRow, [key]: value };
            })
            return nextState;
        })
    }, [primaryKey]);
    return { rowsVisual, modifyRowsVisual }
}
export default useRowVisual;